/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <sys/types.h>
#include <gtest/gtest.h>
#include <dirent.h>
#include <unistd.h>
#include <vector>
#include <string>
#include "It_process_plimits.h"

static std::string GetFileMode(const char* filePath)
{
    const int MODE_COUNT = 11;
    char fileProperty[MODE_COUNT] = "----------";
    char fileMode[MODE_COUNT] = "-rwxrwxrwx";
    struct stat buf;
    stat(filePath, &buf);
    unsigned int off = 256;
    const int LOOP_VARY = 10;
    for (int i = 1; i < LOOP_VARY; i++) {
        if (buf.st_mode & (off >> (i - 1))) {
            fileProperty[i] = fileMode[i];
        }
    }
    return fileProperty;
}

static int IsFileDirectoryRW(const char* filePath)
{
    std::string fileOrg = "-rw-r--r--";
    std::string fileProperty = GetFileMode(filePath);
    return strcmp(fileProperty.c_str(), fileOrg.c_str());
}
static UINT32 Testcase(void)
{
    int ret;
    mode_t mode;
    DIR *dirp = opendir("/proc/plimits");
    ICUNIT_ASSERT_NOT_EQUAL(dirp, NULL, -1);
    (void)closedir(dirp);

    std::string filePath = "/proc/plimits/test";
    std::vector<std::string> fileName;
    ret = mkdir(filePath.c_str(), S_IFDIR | mode);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    DIR *dirl = opendir(filePath.c_str());
    ICUNIT_GOTO_NOT_EQUAL(dirl, NULL, -1, EXIT);
    (void)closedir(dirl);
    
    fileName.push_back("sched.quota");
    fileName.push_back("sched.period");
    fileName.push_back("memory.limit");
    fileName.push_back("pids.max");
    fileName.push_back("plimits.procs");
    fileName.push_back("plimits.limiter_delete");
    fileName.push_back("plimits.limiter_add");

    for (auto iter = fileName.begin(); iter != fileName.end(); ++iter) {
        std::string fileFullPath = filePath + '/' + *iter;
        int ret = IsFileDirectoryRW(fileFullPath.c_str());
        ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
    }

    (void)RmdirTest(filePath);
    return 0;

EXIT:
    (void)RmdirTest(filePath);
    return 0;
}

void ItProcessPlimits036(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_036", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}

